package com.dh.project.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class DateUtil {

    /**
     * date 按格式转为 string
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String parseString(Date date, String pattern) {
        if (date == null || "".equals(date)) {
            return "";
        }
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        return df.format(date);
    }

    /**
     * 时间戳 转 可读日期
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String parseString(String date, String pattern) {
        if (StringUtil.isBlank(date) || StringUtil.isBlank(pattern)) {
            return "";
        }
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        return df.format(date);
    }

    /**
     * date 转 时间戳
     *
     * @param date
     * @return
     */
    public static String dateForString(Date date) {
        Long time = date.getTime();
        return String.valueOf(time);
    }

    /**
     * 时间戳 转 date
     *
     * @param time
     * @return
     */
    public static Date parseDate(long time) {
        return new Date(time);
    }

    /**
     * 时间戳 转 date
     *
     * @param time
     * @return
     */
    public static Date parseDate(String time) {
        return new Date(StringUtil.parseLong(time));
    }

    /**
     * 根据日期转换星期
     *
     * @param date
     * @return
     */
    public static String dateAndday(Date date) {
        String[] weekOfDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }
        int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekOfDays[w];
    }


    /**
     * 获取日期之间天数差
     *
     * @return
     */
    public static int getDateCha(Date fDate, Date oDate) {

        if (null == fDate || null == oDate) {
            return -1;
        }
        long intervalMilli = oDate.getTime() - fDate.getTime();
        return (int) (intervalMilli / (24 * 60 * 60 * 1000));
    }


}
